package org.eclipse.soc.ide4edu.assignment.operations;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

/**
 * This class holds does the work of Generating a complete Login URL, and holds
 * a HashMap to map supported Servers to their Login Page
 * 
 * @author Souvik Roy
 * 
 */
public class LoginMap {
	private HashMap loginMap = new HashMap();

	/**
	 * Constructor which initializes the Hash Map
	 */
	public LoginMap() {
		initializeLoginMap();
	}

	/**
	 * Generates the complete Login URL
	 * 
	 * @param serverURL
	 *            The Server's Home URL
	 * @param serverSelection
	 *            The Server Name
	 * @return the generated Login URL
	 */
	public URL getLoginUrl(String serverURL, String serverSelection) {

		String initialURL = serverURL.trim();
		URL finalURL = null;

		if (initialURL.endsWith("/"))
			initialURL = initialURL.substring(0, initialURL.length() - 1);

		try {
			finalURL = new URL(initialURL + loginMap.get(serverSelection));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		if (finalURL != null)
			return finalURL;

		return null;
	}

	/**
	 * Method which fills the Hash Map to map the Server Name and its Login
	 * String
	 */
	public void initializeLoginMap() {
		loginMap.put(new String("Moodle"), new String("/login/index.php"));
	}

}
